package com.hnevc.yourhealthmanager.controller;

import com.hnevc.yourhealthmanager.dto.ResponseResult;
import com.hnevc.yourhealthmanager.entity.FoodCategory;
import com.hnevc.yourhealthmanager.entity.HealthPlane;
import com.hnevc.yourhealthmanager.service.HealthPlaneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/HealthPlane")
@Api(tags = "收获减肥计划数据接口")
public class HealthPlaneController {
    @Autowired
    private HealthPlaneService healthPlaneService;

    @PostMapping("/add")
    @ApiOperation(value = "addHealthPlane", notes = "添加减肥计划数据信息，跑步，步行，篮球等")
    public ResponseResult addHealthPlane(@RequestBody HealthPlane healthPlane) {
        healthPlaneService.save(healthPlane);
        return ResponseResult.success(healthPlane);
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "getHealthPlaneById", notes = "获取减肥计划数据信息等")
    public ResponseResult getHealthPlaneById(@PathVariable Integer id) {
        HealthPlane categoryServiceById = healthPlaneService.getById(id);
        return ResponseResult.success(categoryServiceById);
    }
    @GetMapping("/list")
    @ApiOperation(value = "listHealthPlane", notes = "获取所有减肥计划数据列表信息")
    public ResponseResult listHealthPlane() {
        List<HealthPlane> healthPlaneList = healthPlaneService.list();
        return ResponseResult.success(healthPlaneList);
    }

    @GetMapping("/listByUserId/{userId}")
    @ApiOperation(value = "listHealthPlane", notes = "获取所有减肥计划数据列表信息")
    public ResponseResult listByUserId(@PathVariable("userId") Integer userId) {
        List<HealthPlane> healthPlaneList = healthPlaneService.getHealthPlaneByUserId(userId);
        return ResponseResult.success(healthPlaneList);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateHealthPlane", notes = "更新减肥计划数据信息")
    public ResponseResult updateHealthPlane(@RequestBody HealthPlane healthPlane) {
        healthPlaneService.updateById(healthPlane);
        return ResponseResult.success(healthPlane);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "deleteHealthPlane", notes = "根据ID删除减肥计划数据信息")
    public ResponseResult deleteHealthPlane(@PathVariable Integer id) {
        healthPlaneService.removeById(id);
        return ResponseResult.success(null);
    }
    @PostMapping("/upload_pic")
    @ApiOperation(value = "上传照片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "照片"),
            @ApiImplicitParam(name = "id",value = "ID")
    })
    public Map<String,Object> uploadPic(@RequestParam("file") MultipartFile file,
                                        @RequestParam("id") Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
            byte[] bytes = file.getBytes();
            String filePath =  new RandomString(10).nextString() + file.getOriginalFilename();
            Path path = Paths.get("D://upload//" +filePath);
            Files.write(path, bytes);
            map.put("success",true);
            map.put("msg","成功");
            //TODO 更新到数据库
            // 1. 获取用户服务对象，
            HealthPlane healthPlane = healthPlaneService.updatePic(id, "/" + filePath);
            map.put("data",healthPlane);

        } catch (IOException e) {
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","文件读取错误");
        }
        return map;
    }
}
